package demo.observer;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : feixiang.li
 * @since : 2025-09-17 16:51
 */
public class TvStation {

    Map<Class<? extends Event>, List<EventListener>> eventListenerMap = new HashMap<>();

    public void subscribe(EventListener listener, Class<? extends Event> clazz) {
        eventListenerMap.computeIfAbsent(clazz, k -> new ArrayList<EventListener>()).add(listener);
    }


    public void pushEvent(Event event) {
        List<EventListener> listeners = eventListenerMap.get(event.getClass());
        if (listeners != null) {
            for (EventListener listener : listeners) {
                listener.onEvent(event);
            }
        }
    }

}
